package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.GrandChildUserExam;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildUserExamDaoImpl extends TempDataObjectDao<GrandChildUserExam> {

    public static String getGrandChildUserExamSql = "select * from `t_u_grand_child_exam`";

    public Map<Long, GrandChildUserExam> getGrandChildUserExamMap() {
        Map<Long, GrandChildUserExam> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGrandChildUserExamSql);
            rs = call.executeQuery();
            while (rs.next()) {
                GrandChildUserExam parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateGrandChildUserExamSql = "update `t_u_grand_child_exam` set resultList=?,noReadNo=? where userId=?";

    @Override
    protected boolean updateData(GrandChildUserExam grandChildUserExam) {
        grandChildUserExam.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(grandChildUserExam.getResultList()))));
        para.put(i++, new DbParameter(Types.VARCHAR, grandChildUserExam.getNoReadNo()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildUserExam.getUserId()));
        if (getDb().executeNoneQuery(updateGrandChildUserExamSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGrandChildUserExamSql = "insert into `t_u_grand_child_exam`(userId,resultList,noReadNo) values (?,?,?)";

    @Override
    protected boolean addData(GrandChildUserExam grandChildUserExam) {
        grandChildUserExam.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, grandChildUserExam.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(new ArrayList<>(grandChildUserExam.getResultList()))));
        para.put(i++, new DbParameter(Types.VARCHAR, grandChildUserExam.getNoReadNo()));
        if (getDb().executeNoneQuery(insertGrandChildUserExamSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GrandChildUserExam parseData(ResultSet rs) throws SQLException {
        GrandChildUserExam grandChildUserExam = new GrandChildUserExam();
        grandChildUserExam.setUserId(rs.getLong("userId"));
        grandChildUserExam.setResultList(JSON.parseObject(rs.getString("resultList"), new TypeReference<java.util.List<com.yanqu.road.entity.grandchild.GrandChildExamResult>>() {
        }));
        grandChildUserExam.setNoReadNo(rs.getString("noReadNo"));
        return grandChildUserExam;
    }

}
